// ==UserScript==
// @name         JAVDB跳转
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://javdb.com/search?*
// @include      https://javdb*.com/search?*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixhost.to
// @grant        none


// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    // alert("javdb start!");
	
	// 检测参数jump=auto
    let jump = getQueryParam("jump");
    if (!jump) return;

    findFrstItem();

    function findFrstItem(){
        // let sreach = document.querySelector('.page-title.ast-archive-title span');
        // let sreachStr = sreach.textContent;

        let sreachStr = getQueryParam("q");
        if (!sreachStr) return;

        let h2Arr = document.querySelectorAll('.movie-list .video-title');
        if (!h2Arr) return;

        let destAaa = null;
        for (let i = 0; i < h2Arr.length; i++) {
            let titleEle = h2Arr[i];
            if (i == 5) break;

            // 筛选匹配的番号数据  mium-999 => 正则/mium.*999/gi
            let reg = RegExp(sreachStr, "gi");
            if (reg.test(titleEle.innerText)) {
                // var fhd_idx = aaa.innerHTML.search(/FHD/i);
                // if (fhd_idx > 0) {
                //     destAaa = aaa;
                //     break;
                // }

                destAaa = titleEle.parentElement;
                break;
            }
        }

        if (destAaa){
            window.location.href = destAaa.href + "?jump=auto";
        }
    }


    function getQueryParam(param) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(param);
    }

})();